import org.doofer.*

/*****************************************************************************************************************************************************
 * This is the cabinet lister script done a few ways using the session-related methods of the Doofer class.
 * You _could_ list cabinets in a single line but legibility would suffer - 2 lines seems short enough anyway :-)
 *
 * In order to run this script you should have the following set up:
 * - DFC installed (the installer should've included the DFC in the CLASSPATH)
 * - The doofer.jar installed to the Groovy lib directory (i.e.%USERPROFILE%\.groovy\lib)
 *******************************************************************************************************************************************************/

 // Get a session, switch to the context of the session then disconnect. The queryForList method is actually a method of the
// IDfSession instance but since we're in the context of the IDfSession instance we can call methods of it without qualification.
// This is a similar idea to Java's static import feature.
new Doofer().withSession(username: args[0], password: args[1], repository: args[2]) {
    queryForList(dql: 'select object_name from dm_cabinet').each { println it.object_name }
}

 // Get a session, use it then disconnect.
new Doofer().useSession(username: args[0], password: args[1], repository: args[2]) { session ->
    session.queryForList(dql: 'select object_name from dm_cabinet').each { println it.object_name }
}

// Get a session, use it then manually disconnect.
def doofer = new Doofer()
def session = doofer.connect(username: args[0], password: args[1], repository: args[2])
if (session) {
    session.queryForList(dql: 'select object_name from dm_cabinet').each { println it.object_name }
    doofer.disconnect(session: session)
}

/*****************************************************************************************************************************************************
 * There are a few things to note in the above code:
 * 1. "new Doofer()" bootstraps Doofer, setting up the DFC extensions
 * 2. The Doofer class is also factory class, generating instances of util classes such as SessionUtil, ObjectUtil, etc
 * 3. The Doofer class is a facade for common session-related activities - e.g. connect, disconnect, useSession, withSession
 *******************************************************************************************************************************************************/
